<?php

namespace App\Http\Controllers;

use App\Models\Area;
use App\Models\Car;
use App\Models\Complaint;
use App\Models\Cts;
use App\Models\Flow;
use App\Models\Gps;
use App\Models\Guide;
use App\Models\Hotel;
use App\Models\Notice;
use App\Models\Scene;
use App\Models\SceneInfo;
use App\Models\Team;
use App\Models\Type;
use Carbon\Carbon;
use DB;
use Illuminate\Support\Facades\Input;

class ApiController extends Controller
{
    public function getScenes()
    {
        $keyword = Input::get('keyword');
        $where = Input::has('keyword')?[['title',$keyword]]:[];
        $list = Scene::where($where)->get()->map(function ($val){
            return [
                'type'=>Type::find($val->type_id)->title,
                'area'=>Area::find($val->area_id)->title,
                'title'=>$val->title,
                'business_license'=>$val->business_license,
                'latlng'=>$val->latlng,
                'level'=>$val->level?config('options.scene_level')[$val->level]:'',
            ];
        });
        return response()->json($list);
    }

    public function getHotel()
    {
        $keyword = Input::get('keyword');
        $where = Input::has('keyword')?[['title',$keyword]]:[];
        $list = Hotel::where($where)->get()->map(function ($val){
            return [
                'area'=>Area::find($val->area_id)->title,
                'title'=>$val->title,
                'address'=>$val->address,
                'latlng'=>$val->latlng,
                'level'=>$val->level?config('options.hotel_star')[$val->level]:'',
                'tel'=>$val->tel
            ];
        });
        return response()->json($list);
    }

    public function getCTS()
    {
        $keyword = Input::get('keyword');
        $where = Input::has('keyword')?[['title',$keyword]]:[];
        $list = Cts::where($where)->get()->map(function ($val){
            //return $val;
            return [
                'area'=>Area::find($val->area_id)->title,
                'title'=>$val->title,
                'legal_person'=>$val->legal_person,
                'address'=>$val->address,
                'latlng'=>$val->latlng,
                'level'=>$val->level?config('options.cts_level')[$val->level]:'',
                'licence'=>$val->licence,
                'credit_code'=>$val->credit_code
            ];
        });
        return response()->json($list);
    }

    public function getTeams()
    {
        $keyword = Input::get('keyword');
        $date = Input::get('date');
        $where = Input::has('keyword')?[['title',$keyword]]:[];
        $date = Input::has('date')?$date:Carbon::today()->toDateString();
        $list = Team::select(['team_id','origin','destination','guide','travel_agency','transport_company','car_id','arrive_date',DB::raw('Count(id) as count')])
            ->where($where)
            ->where('arrive_date',$date)
            ->groupBy('team_id')
            ->get();
        return response()->json($list);
    }

    public function getGuides(){
        $keyword = Input::get('keyword');
        $where = Input::has('keyword')?[['name',$keyword]]:[];
        $list = Guide::where($where)->get()->map(function ($val){
            return [
                'name'=>$val->name,
                'ID'=>$val->id_code,
                'qualifications_code'=>$val->qualifications_code,
                'phone'=>$val->phone,
                'sex'=>config('options.sex')[$val->sex],
                'level'=>$val->level?config('options.guide_level')[$val->level]:'',
                'languages'=>config('options.guides_languages')[$val->languages]
            ];
        });
        return response()->json($list);
    }

    public function getCars(){
        $keyword = Input::get('keyword');
        $where = Input::has('keyword')?[['name',$keyword]]:[];
        $list = Car::where($where)->get()->map(function ($val){
            $scope = collect(explode(',',$val->scope))->map(function ($val){
                return config('options.car_scope')[$val];
            });
            return [
                'area'=>Area::find($val->area_id)->title,
                'license_plate_number'=>$val->license_plate_number,
                'company'=>$val->company,
                'scope'=>$scope
            ];
        });
        return response()->json($list);
    }

    public function getSceneMerchant(){
        $list = SceneInfo::get()->map(function ($val){
            return [
                'scene'=>Scene::find($val->scene_id)->title,
                'hotel'=>$val->hotel,
                'shop'=>$val->shop,
                'restaurant'=>$val->restaurant,
                'car_park'=>$val->carpark,
                'toilet'=>$val->toilet,
                'vav'=>$val->vav,
                'monitor'=>$val->camera,
                'amusement'=>$val->amusement,
            ];
        });
        return response()->json($list);
    }

    public function gps(){
        $list = Gps::all();
        foreach ($list as $val){
            $val->VEHICLE_COLOR = config('options.vehicle_color')[$val->VEHICLE_COLOR];
            $val->ALARM = strpos($val->ALARM,'1')?true:false;
        }
        return response()->json($list);
    }

    public function waterWorldFlows(){
        $scene_id=15;
        $list = Flow::where('scene_id',$scene_id)->get(['date','in','out','num'])->groupBy('date');
        return response()->json($list);
    }

    public function waterWorldOperation(){
        $scene_id=15;
        $list = SceneInfo::where('scene_id',$scene_id)->get();
        $complaint = Complaint::where([['target','scenes'],['target_id',$scene_id]])->get();
        return response()->json([
            'Infrastructure'=>$list,
            'complaint'=>$complaint
        ]);
    }

    public function peachGardenFlows(){
        $scene_id=3;
        $list = Flow::where('scene_id',$scene_id)->get(['date','in','out','num'])->groupBy('date');
        return response()->json($list);
    }
    public function peachGardenOperation(){
        $scene_id=3;
        $list = SceneInfo::where('scene_id',$scene_id)->get();
        $complaint = Complaint::where([['target','scenes'],['target_id',$scene_id]])->get();
        return response()->json([
            'Infrastructure'=>$list,
            'complaint'=>$complaint
        ]);
    }

    public function release(){
        $type =Input::get('type');
        if(Input::has('type')){
            $notice = Notice::where('release','like','%'.$type.'%')->get();
            return response()->json($notice);
        }else
            return response()->json(false);

    }

    public function get_dh_list(){
        $https = get_dh_token();
        $token = $https['access_token'];
        $expires_in = ceil($https['expires_in']/60);
        $data = [
            'pageNum'=>1,
            'pageSize'=>10
        ];
        $headers = [
            'Content-Type:application/json;charset=utf-8',
            'Authorization:Bearer '.$token
        ];
        $lechange_https = post_curls('https://www.cloud-dahua.com/gateway/device/api/lechangeToken',[],$headers);
        $lechange_token = $lechange_https['data']['token'];
        $liveList_http = post_curls('https://www.cloud-dahua.com/gateway/device/api/page/liveList',['token'=> $lechange_token,'queryRange'=>'1-100'],$headers);
        return response()->json($liveList_http);


//        $info = getDHToken(env('DH_CLIENT_ID') , env('DH_CLIENT_SECRET'));
//        $info = json_decode($info);
//        $dhtoken = $info->access_token;
//        $info = getDHuserToken($dhtoken , env('DH_PHONE'));
//
//        $info = json_decode($info);
//        $dhusertoken = $info->data;
//        $info = getDHdeviceList($dhtoken , $dhusertoken);
//        $info = json_decode($dhusertoken);
//
//        if(property_exists($info, 'code')){
//            if($info->code=='0'){
//                $videos = [];
//                $list = $info->data->lives;
//                foreach ($list as $v){
//
//                    if($v->liveStatus===1){
//                        $vo['status'] = $v->liveStatus;
//                    }else{
//                        $vo['status'] = 0;
//                    }
//                    $vo['deviceid'] = $v->deviceId;
//                    $vo['channel']  = $v->channelId;
//                    if(property_exists($v, 'streams')){
//                        if(property_exists($v->streams[0] , 'coverUrl')){
//                            $vo['cover'] = $v->streams[0]->coverUrl;
//                        }else{
//                            $vo['cover'] = '';
//                        }
//                        if(property_exists($v->streams[0] , 'hls')){
//                            $vo['hls'] = $v->streams[0]->hls;
//                        }else{
//                            $vo['hls'] = '';
//                        }
//                    }else{
//                        $vo['cover'] = '';
//                        $vo['hls'] = '';
//                    }
//                    $videos[] = $vo;
//                    unset($vo);
//                }
//            }else{
//                \Cache::store('file')->put('dhusertoken', '', 60);
//                \Cache::store('file')->put('dhtoken', '', 60);
//                $videos = [];
//            }
//        }else{
//            \Cache::store('file')->put('dhusertoken', '', 60);
//            \Cache::store('file')->put('dhtoken', '', 60);
//            $videos = [];
//        }
//        return $videos;
    }
}
